Learn R Programming

NMF (version 0.5.06)

Sub-setting NMF objects: Subset method for objects of class NMF

Description

This method provide a convenient way of sub-setting objects of class NMF, using a matrix-like syntax.

It allows to consistently subset one or both matrix factors in the NMF model, as well as retrieving part of the basis components or part of the mixture coefficients with a reduced amount of code. Read section Value for more details on the returned value.

Usage

## S3 method for class 'NMF':
[(x, i, j, ..., drop=FALSE)

Arguments

x
an object of class NMF to be subset.
i
index used to subset on the rows of the basis matrix (i.e. the features). It can be a numeric, logical, or character vector (whose elements must match the row names of x). In the cas
j
index used to subset on the columns of the mixture coefficient matrix (i.e. the samples). It can be a numeric, logical, or character vector (whose elements must match the column names of
...
used to specify a third index to subset on the basis components, i.e. on both the columns and rows of the basis matrix and mixture coefficient respectively. It can be a numeric, logical, or character vector (whose
drop
single logical value used to drop the NMF-class wrapping and extract parts of the factor matrices. When drop=FALSE it returns the NMF object x with the basis matrix and/or mixture

Value

  • The returned value depends on the number of subset index passed and the value of argument drop:
    • No index as inx[]orx[,]: the value is the objectxunchanged.
    • One single index as inx[i]: the value is the basis matrix subset byi. Precisely the callx[i]is equivalent tobasis(x)[, i, drop=TRUE]. If argumentdropis present then it is used:x[i, drop=TRUE.or.FALSE]<=>basis(x)[, i, drop=TRUE.or.FALSE].
    • More than one index withdrop=FALSE(default) as inx[i,j],x[i,],x[,j],x[i,j,k],x[i,,k], etc...: the value is aNMFobject whose basis and/or mixture coefficient matrices have been subset accordingly. The third indexkaffects simultaneously the columns of the basis matrix AND the rows of the mixture coefficient matrix.
    • More than one index withdrop=TRUEandixorjmissing: the value returned is the matrix that is the more affected by the subset index. That is thatx[i, , drop=TRUE]andx[i, , k, drop=TRUE]return the basis matrix subset by[i,]and[i,k]respectively, whilex[, j, drop=TRUE]andx[, j, k, drop=TRUE]return the mixture coefficient matrix subset by[,j]and[k,j]respectively.

Examples

Run this code
# create a dummy NMF object that highlight the different way of subsetting
a <- nmfModel(W=outer(seq(1,5),10^(0:2)), H=outer(10^(0:2),seq(-1,-10)))
basisnames(a) <- paste('b', 1:nbasis(a), sep='')
rownames(a) <- paste('f', 1:nrow(a), sep='')
colnames(a) <- paste('s', 1:ncol(a), sep='')

# or alternatively:
# dimnames(a) <- list( features=paste('f', 1:nrow(a), sep=''), samples=paste('s', 1:ncol(a), sep=''), basis=paste('b', 1:nbasis(a)) )

# look at the resulting NMF object 
a
basis(a)
coef(a)

# extract basis components
a[1]
a[1, drop=FALSE] # not dropping matrix dimension
a[2:3]

# subset on the features
a[1,]
a[2:4,]
# dropping the NMF-class wrapping => return subset basis matrix
a[2:4,, drop=TRUE]

# subset on the samples
a[,1]
a[,2:4]
# dropping the NMF-class wrapping => return subset coef matrix
a[,2:4, drop=TRUE]

# subset on the basis => subsets simultaneously basis and coef matrix
a[,,1]
a[,,2:3]
a[4:5,,2:3]
a[4:5,,2:3, drop=TRUE] # return subset basis matrix
a[,4:5,2:3, drop=TRUE] # return subset coef matrix

# 'drop' has no effect here
a[,,2:3, drop=TRUE]

Run the code above in your browser using DataLab